using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEngine.ParticleSystem;

public class SkillTornado211 : SkillBasic
{
    // Start is called before the first frame update
    public int face;
    public float CD;
    private float CDtime;
    Transform playertransform;
    GameObject playerobject;
    PlayerBasic player;
    public GameObject tornado;
    bool isskilling;
    public float skilltime;
    public float deltadistance;
    float distance;
    float distancememory;
    //public PlayerAttack playerattack;
    //private Rigidbody2D myRigidbody;
    void Start()
    {
        playertransform = GetComponentInParent<Transform>();
        player = GetComponentInParent<PlayerBasic>();
        TornadoCD.CD = CD;
        TornadoCD.CDCurrent = 0;
    }
    void Skill()
    {
        Vector3 position = playertransform.position;
        position.y = 5.0f;
        if (face == -1)
        {

            Instantiate(tornado, position, Quaternion.identity);
        }
        else
        {
            Instantiate(tornado, position, Quaternion.Euler(0, 180, 0));
        }
    }
    void skillend()
    {
        isskilling = false;
        distance = 0;
    }
    public IEnumerator Skilling()
    {
        CDtime = CD;
        isskilling = true;
        distance = 0;
        distancememory = playertransform.position.x;
        yield return new WaitForSeconds(skilltime);
        skillend();

    }
    // Update is called once per frame
    void Update()
    {
        face = player.face;
        if (CDtime > 0)
        {
            CDtime -= Time.deltaTime;
            TornadoCD.CDCurrent = CDtime;
        }
        if (Input.GetKeyDown(KeyCode) && Time.timeScale > 0)
        {
            if (CDtime <= 0 && player.movable)
            {
                isskilling = true;
                StartCoroutine(Skilling());
            }


        }
        if (isskilling==true)
        {
            distance +=Mathf.Abs(distancememory-playertransform.position.x);
            distancememory = playertransform.position.x;
            while (distance >= deltadistance)
            {
                distance -= deltadistance;
                Skill();
            }
        }
    }
}
